# -*- mode: python -*-
###
#
# (c) 2000-2007 Wijnand Modderman <wijnand at tehmaze dot com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

__revision__ = '$Hg:$'

# system imports
import time

# DDB imports
from ddb.log import log

class conferenceManager:
    def __init__(self):
        self.conferences = {}

    #
    # Generic conference management
    #

    def add(self, name, **settings):
        self.conferences[name.lower()] = {
            'joined':   time.time(),
            'settings': settings,
            'users':    {}
            }
        log.msg('Added conference %s' % name, debug=True)

    def get(self, name, key=None):
        name = name.lower()
        if self.conferences.has_key(name):
            if key:
                return self.conferences[name]['settings'][key]
            else:
                return self.conferences[name]['settings']
    
    def remove(self, name):
        name = name.lower()
        if self.conferences.has_key(name):
            del self.conferences[name]
        log.msg('Removed conference %s' % name, debug=True)

    def flush(self):
        # hmm python doesn't like the dict to be changed while being iterated
        names = self.conferences.keys()
        for name in names:
            del self.conferences[name]
        log.msg('Flushed conferences', debug=True)

    def set(self, name, **settings):
        name = name.lower()
        if not self.conferences.has_key(name):
            self.add(name)
        print 'UPDATE', name, settings
        # update the settings dict
        self.conferences[name]['settings'].update(settings)

    #
    # Users
    #

    def userAdd(self, name, user):
        name = name.lower()
        if not self.conferences.has_key(name):
            self.add(name)
        self.conferences[name]['users'][user] = {
            'joined':   time.time()
            }
    
    def userGet(self, name, user):
        if not self.conferences.has_key(name):
            return
        if self.conferences[name]['users'].has_key(user):
            return self.conferences[name]['users'][user]
        return None 

    def userLocate(self, user):
        locations = []
        for name in self.conferences.keys():
            if user in self.conferences[name]['users']:
                locations.append(name)
        return locations

    def userRemove(self, name, user):
        name = name.lower()
        if not self.conferences.has_key(name):
            return
        if self.conferences[name]['users'].has_key(user):
            del self.conferences[name]['users'][user]


# vim:ts=4:sw=4:et:
